package com.edu.service.impl;

import com.edu.bean.User;
import com.edu.mapper.UserMapper;
import com.edu.service.UserService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public String register(User user) {
        System.out.println("-----------------------------------------------------");
        System.out.println(user.getUserName());
        if (userMapper.selectUserByUserName(user.getUserName()) == null) {
            userMapper.insert(user);
            return "注册成功";
        }
        return "注册失败账户相同";
    }

    @Override
    public Map<String, Object> login(User user) {
        Map<String,Object> userMap = new HashMap<>();
        if (userMapper.selectUserByUserName(user.getUserName()) != null) {
            User user1 = userMapper.selectUserByUserName(user.getUserName());
            if (user1.getUserPass().equals(user.getUserPass())) {
                userMap.put("msg",user);
            }
                userMap.put("msg","密码错误");
        }
        userMap.put("msg","账户不存在");
        return userMap;
    }

    @Override
    public String changePass(User user, String newPass) {
        if (userMapper.selectUserByUserName(user.getUserName()) != null) {
            User user1 = userMapper.selectUserByUserName(user.getUserName());
            if (user1.getUserPass().equals(user.getUserPass())) {
                user.setUserPass(newPass);
                int row = userMapper.updatePassByName(user);
                if (row > 0) {
                    return "修改成功";
                } else {
                    return "修改失败";
                }
            }
            return "老密码错误";
        }
        return "用户不存在";
    }


}
